#region Copyright Notice

// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 

#endregion

using System;
using System.Runtime.InteropServices;

namespace AbstractClass
{
    /// <summary>
    /// Contains P/Invoke signatures
    /// </summary>
    internal static class NativeMethods
    {
        [DllImport("shell32.dll", EntryPoint = "SHFileOperationW", CallingConvention = CallingConvention.StdCall)]
        internal static extern int SHFileOperationW(ref SHFILEOPSTRUCT lpFileOp);

        [DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl,
            SetLastError = false)]
        public static extern IntPtr MemCopy(IntPtr dest, IntPtr src, UIntPtr count);

        [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
        internal static extern int SHQueryRecycleBin(string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);

        [DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
        internal static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);

        [DllImport("urlmon.dll", CharSet = CharSet.Unicode, ThrowOnUnmappableChar = true)]
        internal static extern UInt32 FindMimeFromData(
            IntPtr pBc,
            [MarshalAs(UnmanagedType.LPWStr)] String pwzUrl,
            [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
            UInt32 cbSize,
            [MarshalAs(UnmanagedType.LPWStr)] String pwzMimeProposed,
            UInt32 dwMimeFlags,
            out UInt32 ppwzMimeOut,
            UInt32 dwReserverd
            );

        [DllImport("shell32.dll", EntryPoint = "ExtractIconA", CharSet = CharSet.Unicode, SetLastError = true,
            ExactSpelling = true)]
        internal static extern IntPtr ExtractIcon(int hInst, string lpszExeFileName, int nIconIndex);

        [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
        internal static extern uint ExtractIconEx(string szFileName, int nIconIndex, IntPtr[] phiconLarge,
                                                  IntPtr[] phiconSmall, uint nIcons);

        [DllImport("user32.dll", EntryPoint = "DestroyIcon", SetLastError = true)]
        internal static extern int DestroyIcon(IntPtr hIcon);

        [DllImport("shell32.dll", EntryPoint = "SHGetFileInfoW", CallingConvention = CallingConvention.StdCall)]
        internal static extern uint SHGetFileInfoW([In] [MarshalAs(UnmanagedType.LPWStr)] string pszPath,
                                                   uint dwFileAttributes, ref SHFILEINFOW psfi, uint cbFileInfo,
                                                   FileInfoOption uFlags);
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    internal struct SHFILEOPSTRUCT
    {
        public IntPtr hwnd;
        [MarshalAs(UnmanagedType.U4)] public FileOperation wFunc;
        [MarshalAs(UnmanagedType.LPWStr)] public string pFrom;
        [MarshalAs(UnmanagedType.LPWStr)] public string pTo;
        public OperationFlag fFlags;
        [MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted;
        public IntPtr hNameMappings;
        [MarshalAs(UnmanagedType.LPWStr)] public string lpszProgressTitle;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
    internal struct SHQUERYRBINFO
    {
        public Int32 cbSize;
        public UInt64 i64Size;
        public UInt64 i64NumItems;
    }

    [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct SHFILEINFOW
    {
        public IntPtr hIcon;
        public int iIcon;
        public uint dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName;
    } ;

    internal enum FileOperation
    {
        Delete = 3
    }

    [Flags]
    internal enum OperationFlag : ushort
    {
        AllowUndo = 64,
        NoConfirmation = 16
    }

    [Flags]
    internal enum RecycleFlags : uint
    {
        NoConfirmation = 0x00000001,
        NoProgressUI = 0x00000002,
        NoSound = 0x00000004
    }

    [Flags]
    internal enum FileInfoOption : uint
    {
        DisplayName = 0x00000200,
        TypeName = 0x400,
        Icon = 0x100,
        LargeIcon = 0x0,
        SmallIcon = 0x1,
        UseFileAttributes = 0x10
    }
}